package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.vo.BaseData;
import com.cskaoyan.model.admin.vo.BaseRespVo;
import com.cskaoyan.service.admin.GoodsAdminKeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("admin/keyword")
public class KeywordController {

    @Autowired
    GoodsAdminKeywordService goodsAdminKeywordService;
    @RequestMapping("list")
    public BaseRespVo list(BaseParam baseParam,String keyword,String url){

        BaseData data = goodsAdminKeywordService.list(baseParam, keyword, url);

        return BaseRespVo.ok(data);
    }

    @PostMapping("create")
    public BaseRespVo create(@RequestBody MarketKeyword marketKeyword){
        marketKeyword.setAddTime(new Date());
        marketKeyword.setUpdateTime(new Date());
        MarketKeyword keywordGoods = goodsAdminKeywordService.create(marketKeyword);
        if(keywordGoods == null)return BaseRespVo.fail(404,"关键字相关信息插入有误");
        return BaseRespVo.ok(keywordGoods);

    }
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketKeyword marketKeyword){
        marketKeyword.setUpdateTime(new Date());
        MarketKeyword keywordGoods = goodsAdminKeywordService.update(marketKeyword);
        if(keywordGoods != null){
            return BaseRespVo.ok(keywordGoods);
        }return BaseRespVo.fail(404,"关键字相关信息修改失败");
    }
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketKeyword marketKeyword){
        marketKeyword.setUpdateTime(new Date());
        Integer row = goodsAdminKeywordService.delete(marketKeyword.getId());
        if(row == 1){
            return BaseRespVo.ok("关键字相关信息删除成功");
        }return BaseRespVo.fail(404,"关键字相关信息删除失败");
    }

}
